<template>
  <div>
    <el-form :inline="true" :model="goods" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input v-model="goods.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌">
        <el-select v-model="goods.brandId" placeholder="品牌">
          <el-option  v-for="b in brands" :label="b.name" :value="b.id" :key="b.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="id"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="商品名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="pic"
          label="商品图片">
        <template v-slot="s">
          <el-image :src="s.row.pic" style="width: 50px;height: 30px" > </el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="price"
          label="价格/货号"
          width="180">
        <template v-slot="s">
          价格:{{s.row.price}}<br>
          货号:{{s.row.hh}}
        </template>
      </el-table-column>
      <el-table-column
          prop="num"
          label="销量"
          width="180">
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
          width="180">
      </el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="delById(scope.row.id)" type="text" size="small">删除</el-button>

        </template>
      </el-table-column>
    </el-table>


    <el-pagination

        @current-change="goPage"

        :page-size="3"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "GoodsView",
  data() {
    return {
      tableData:[],
      goods:{},
      total:0,
      pageNum:1,
      brands:[]
    }
  }, methods: {
    delById(id){
      this.axios.post("http://localhost:9999/goods/delById?id="+id).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    goPage(page){
      this.pageNum =page;
      this.list();
    },
    list(){
      this.axios.post("http://localhost:9999/goods/list?pageNum="+this.pageNum,this.goods).then(res=>{
        this.tableData = res.data.data.list;
        this.total =res.data.data.total;
      })
    },getBrands(){
      this.axios.get("http://localhost:9999/goods/brandsList").then(res=>{
        this.brands =res.data.data;
      })
    }
  },
  created() {
    this.list();
    this.getBrands()
  }
}
</script>

<style scoped>

</style>